package com.sunlake.spring.main.model.account.controller;

import com.sunlake.spring.main.common.response.R;
import com.sunlake.spring.main.common.utils.CurrentUserUtil;
import com.sunlake.spring.main.model.account.dto.AccountRechargeDTO;
import com.sunlake.spring.main.model.account.dto.FreezeBalanceDTO;
import com.sunlake.spring.main.model.account.dto.RefundToBalanceDTO;
import com.sunlake.spring.main.model.account.dto.UnfreezeBalanceDTO;
import com.sunlake.spring.main.model.account.service.AccountService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 账户控制器
 * 处理账户相关的请求
 * @author by liuhanzhi
 */
@RestController
@RequestMapping("/api/account")
@Slf4j
public class AccountController {
    @Autowired
    private AccountService accountService;

    /**
     * 获取用户账户余额
     *
     * @return 账户余额
     */
    @GetMapping("/balance")
    public R getAccountBalance() {
        Long userId= CurrentUserUtil.getCurrentUserId();
        if(userId==null){
            return R.error(400,"用户未登录");
        }
        return accountService.getAccountBalance(userId);
    }

    @PostMapping("/recharge")
    public R recharge(@Valid @RequestBody AccountRechargeDTO accountRechargeDTO) {
        Long userId= CurrentUserUtil.getCurrentUserId();
        if(userId==null){
            return R.error(400,"用户未登录");
        }
        return accountService.recharge(userId, accountRechargeDTO);
    }

    @GetMapping("/balance-records")
    public R getAccountBalanceRecords(
            @RequestParam(required = false) String recordType,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Long userId = CurrentUserUtil.getCurrentUserId();
        if (userId == null) {
            return R.error(400, "用户未登录");
        }
        LocalDateTime startDateTime = null;
        LocalDateTime endDateTime = null;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if (startDate != null && !startDate.isEmpty()) {
            startDateTime = LocalDateTime.parse(startDate + " 00:00:00", formatter);
        }
        if (endDate != null && !endDate.isEmpty()) {
            endDateTime = LocalDateTime.parse(endDate + " 23:59:59", formatter);
        }
        return accountService.getAccountBalanceRecords(userId, recordType, startDateTime, endDateTime, page, pageSize);
    }
    @GetMapping("/consumption-records")
    public R getAccountConsumptionRecords() {
        Long userId= CurrentUserUtil.getCurrentUserId();
        if(userId==null){
            return R.error(400,"用户未登录");
        }
        return accountService.getAccountConsumptionRecords(userId);
    }
    /**
     * 检查充值订单状态
     */
    @GetMapping("/recharge/{rechargeNo}/status")
    public R checkRechargeStatus(@PathVariable String rechargeNo) {
        return accountService.checkRechargeStatus(rechargeNo);
    }

    /**
     * 冻结余额
     */
    @PostMapping("/balance/freeze")
    public R freezeBalance(@RequestBody FreezeBalanceDTO freezeDTO) {
        Long userId = CurrentUserUtil.getCurrentUserId();
        if (userId == null) {
            return R.error(400, "用户未登录");
        }
        return accountService.freezeBalance(userId, freezeDTO);
    }

    /**
     * 解冻余额
     */
    @PostMapping("/balance/unfreeze")
    public R unfreezeBalance(@RequestBody UnfreezeBalanceDTO unfreezeDTO) {
        Long userId = CurrentUserUtil.getCurrentUserId();
        if (userId == null) {
            return R.error(400, "用户未登录");
        }
        return accountService.unfreezeBalance(userId, unfreezeDTO);
    }

    /**
     * 余额返还/退款
     */
    @PostMapping("/balance/refund")
    public R refundToBalance(@RequestBody RefundToBalanceDTO refundDTO) {
        Long userId = CurrentUserUtil.getCurrentUserId();
        if (userId == null) {
            return R.error(400, "用户未登录");
        }
        return accountService.refundToBalance(userId, refundDTO);
    }
}